package com.shuaiche.yeguan.ui.setting;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

import com.shuaiche.yeguan.R;

public class PermissionSettingActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvPhoneState;
    TextView tvWriteRead;
    TextView tvLocation;
    TextView tvNotify;
    TextView tvCamare;
    TextView tvAudio;
    private RelativeLayout rl_back;
    private LinearLayout ll_phone_state;
    private LinearLayout ll_write_read;
    private LinearLayout ll_location;
    private LinearLayout ll_notify;
    private LinearLayout ll_camare;
    private LinearLayout ll_audio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_permission_setting);


        initViews();
        initPermission();
    }

    private void initViews(){
        tvPhoneState = findViewById(R.id.tv_phone_state);
        tvWriteRead = findViewById(R.id.tv_write_read);
        tvLocation = findViewById(R.id.tv_location);
        tvNotify = findViewById(R.id.tv_notify);
        tvCamare = findViewById(R.id.tv_camare);
        tvAudio = findViewById(R.id.tv_audio);
        rl_back = findViewById(R.id.rl_back);
        ll_phone_state = findViewById(R.id.ll_phone_state);
        ll_write_read = findViewById(R.id.ll_write_read);
        ll_location = findViewById(R.id.ll_location);
        ll_notify = findViewById(R.id.ll_notify);
        ll_camare = findViewById(R.id.ll_camare);
        ll_audio = findViewById(R.id.ll_audio);

        rl_back.setOnClickListener(this);
        ll_phone_state.setOnClickListener(this);
        ll_write_read.setOnClickListener(this);
        ll_location.setOnClickListener(this);
        ll_notify.setOnClickListener(this);
        ll_camare.setOnClickListener(this);
        ll_audio.setOnClickListener(this);
    }

    private void initPermission() {
        //手机状态信息权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED){
            tvPhoneState.setText("已开启");
            tvPhoneState.setTextColor(Color.parseColor("#999999"));
        }else {
            tvPhoneState.setText("去设置");
            tvPhoneState.setTextColor(Color.parseColor("#ff6600"));
        }

        //读写权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
            tvWriteRead.setText("已开启");
            tvWriteRead.setTextColor(Color.parseColor("#999999"));
        }else {
            tvWriteRead.setText("去设置");
            tvWriteRead.setTextColor(Color.parseColor("#ff6600"));
        }

        //位置信息权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            tvLocation.setText("已开启");
            tvLocation.setTextColor(Color.parseColor("#999999"));
        }else {
            tvLocation.setText("去设置");
            tvLocation.setTextColor(Color.parseColor("#ff6600"));
        }

        //通知权限
        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        boolean isOpened = manager.areNotificationsEnabled();
        if (isOpened) {
            tvNotify.setText("已开启");
            tvNotify.setTextColor(Color.parseColor("#999999"));
        }else {
            tvNotify.setText("去设置");
            tvNotify.setTextColor(Color.parseColor("#ff6600"));
        }

        //相机权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
            tvCamare.setText("已开启");
            tvCamare.setTextColor(Color.parseColor("#999999"));
        }else {
            tvCamare.setText("去设置");
            tvCamare.setTextColor(Color.parseColor("#ff6600"));
        }

//        //麦克风权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED){
            tvAudio.setText("已开启");
            tvAudio.setTextColor(Color.parseColor("#999999"));
        }else {
            tvAudio.setText("去设置");
            tvAudio.setTextColor(Color.parseColor("#ff6600"));
        }
//
//        //通讯录权限
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED){
//            tvConcats.setText("已开启");
//            tvConcats.setTextColor(Color.parseColor("#999999"));
//        }else {
//            tvConcats.setText("去设置");
//            tvConcats.setTextColor(Color.parseColor("#ff6600"));
//        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_back:
                finish();
                break;
            case R.id.ll_phone_state:
            case R.id.ll_write_read:
            case R.id.ll_location:
            case R.id.ll_notify:
            case R.id.ll_camare:
            case R.id.ll_audio:
                gotoSetting();
                break;
        }
    }


    public void gotoSetting() {
        Intent intent = new Intent();
        try {
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", "com.shuaiche.yeguan", null);
            intent.setData(uri);
            startActivity(intent);

        } catch (Exception e) {//抛出异常就直接打开设置页面
            intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
            startActivity(intent);
        }
    }
}